package com.pn.controller;

import com.pn.dto.AssignAuthDto;
import com.pn.entity.Result;
import com.pn.model.Role;
import com.pn.page.Page;
import com.pn.service.RoleService;
import com.pn.utils.CurrentUser;
import com.pn.utils.TokenUtils;
import com.pn.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:RoleController
 * Package:com.pn.controller
 * Description:
 *
 * @date:2024/12/29 19:50
 * @author:bailihua
 */
@RequestMapping("/role")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 查询所有角色
     * @return
     */
    @RequestMapping("/role-list")
    public Result roleList(){
        //执行业务
        List<Role> roleList =  roleService.queryAllRole();
        //响应
        return Result.ok(roleList);
    }

    /**
     * 分页查询角色
     * @param page
     * @param role
     * @return
     */
    @RequestMapping("/role-page-list")
    public Result roleListPage(Page page, Role role){
        //执行业务
        page = roleService.queryRolePage(page,role);
        //响应
        return Result.ok(page);
    }

    @RequestMapping("/role-add")
    public Result addRole(@RequestBody Role role, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //拿到当前登录用户的id
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int createBy = currentUser.getUserId();

        role.setCreateBy(createBy);

        //执行业务
        Result result = roleService.saveRole(role);
        //响应
        return result;

    }

    /**
     * 启用或者禁用角色
     * @param role
     * @return
     */
    @RequestMapping("/role-state-update")
    public Result updateRoleState(@RequestBody Role role){
        //执行业务
        Result result = roleService.setRoleState(role);
        //响应
        return result;
    }
    @RequestMapping("/role-delete/{roleId}")
    public Result deleteRole(@PathVariable Integer roleId){
        //执行业务
        Result result = roleService.deleteRoleById(roleId);
        //响应
        return result;
    }

    /**
     * 查询角色分配的所有权限菜单
     * @param roleId
     * @return
     */
    @RequestMapping("/role-auth")
    public Result roleAuth(Integer roleId){
        //执行业务
        List<Integer> authIdList = roleService.queryRoleAuthIds(roleId);
        //响应
        return Result.ok(authIdList);
    }

    @RequestMapping("/auth-grant")
    public Result grantAuth(@RequestBody AssignAuthDto assignAuthDto){
        //执行业务
        roleService.saveRoleAuth(assignAuthDto);
        //响应
        return Result.ok("权限分配成功！");
    }

    /**
     * 修改角色
     * @param role
     * @param token
     * @return
     */
    @RequestMapping("/role-update")
    public Result updateRole(@RequestBody Role role,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME)String token){
        //拿到当前登录用户的id
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int updateBy = currentUser.getUserId();

        role.setUpdateBy(updateBy);

        //执行业务
        Result result = roleService.setRoleByRid(role);
        //响应
        return result;
    }

}
